import _ from 'lodash'
// 下载文件
export const download = (content, filename) => {
  if (!content || !filename) {
    return
  }
  // 创建隐藏的可下载链接
  var eleLink = document.createElement('a');
  eleLink.download = filename;
  eleLink.style.display = 'none';
  // 字符内容转变成blob地址
  eleLink.href = URL.createObjectURL(content);
  // 触发点击
  document.body.appendChild(eleLink);
  eleLink.click();
  // 然后移除
  document.body.removeChild(eleLink);
}

export const downloadByUrl = (url, params = {}) => {
  let targetUrl = url
  const filterParamsKey = Object.keys(params).filter(key => params[key] !== undefined && params[key] !== null) // 过滤空值
  if (filterParamsKey && filterParamsKey.length) {
    targetUrl = targetUrl + '?'
    filterParamsKey.forEach((key, index) => {
      targetUrl = `${targetUrl}${index > 0 ? '&' : ''}${key}=${params[key]}`
    })
  }
  var user = JSON.parse(localStorage.getItem('saber-userInfo'));
  targetUrl = `${targetUrl}&blade-auth=bearer ${user['content'].accessToken}`
  window.open(targetUrl)
  console.log(targetUrl)
}

export const downloadTemplate = (file) => {
  window.open(`${window.location.origin}/template/${file}`)
}